package com.yunlankeji.stemcells.adapter;

import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.stemcells.model.request.Organization_mian;

import java.util.ArrayList;
import java.util.List;

public class Organization_mainAdapter extends RecyclerView.Adapter<Organization_mainAdapter.MyViewholder> implements View.OnClickListener{

    List<Organization_mian>organization_mianList;

    public Organization_mainAdapter(List<Organization_mian> organization_mianList) {
        this.organization_mianList = organization_mianList;
    }

    @NonNull
    @Override
    public MyViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_organization,parent,false);
        MyViewholder myViewHolder = new MyViewholder(itemView);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewholder holder, int position) {
        Organization_mian organization_mian = organization_mianList.get(position);
        holder.tv_organization_name.setText(organization_mian.getTv_organization_name());
        holder.tv_organization_fans.setText(organization_mian.getTv_organization_fans());
        holder.iv_oeganization.setImageResource(organization_mian.getIv_organization());

        holder.itemView.setTag(position);
        holder.lt_organization.setTag(position);

    }

    @Override
    public int getItemCount() {
        return organization_mianList.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();      //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (v.getId()){
                /*case R.id.lt_address_edit:
                    mOnItemClickListener.onItemClick(v, AddressAdapter.ViewName.PRACTISE, position);
                    break;*/
                default:
                    mOnItemClickListener.onItemClick(v, Organization_mainAdapter.ViewName.ITEM, position);
                    break;
            }
        }
    }


    class MyViewholder extends RecyclerView.ViewHolder {

        TextView tv_organization_name;
        TextView tv_organization_fans;
        ImageView iv_oeganization;
        LinearLayout lt_organization;

        public MyViewholder(View itemView) {
            super(itemView);
            this.tv_organization_name = itemView.findViewById(R.id.tv_organization_name);
            this.tv_organization_fans = itemView.findViewById(R.id.tv_organization_fans);
            this.iv_oeganization = itemView.findViewById(R.id.iv_organization);
            this.lt_organization = itemView.findViewById(R.id.lt_organization);

            lt_organization.setOnClickListener(Organization_mainAdapter.this);
        }
    }

    public enum ViewName {
        ITEM,
        PRACTISE
    }

    public interface OnItemClickListener  {
        void onItemClick(View v, Organization_mainAdapter.ViewName viewName, int position);
        void onItemLongClick(View v);
    }

    private Organization_mainAdapter.OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(Organization_mainAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener  = listener;
    }

}
